package org.belkodevelop.report.pdf.builder;

import com.googlecode.charts4j.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;

import de.toolforge.googlechartwrapper.util.MiscUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.belkodevelop.journal.domain.JournalItem;
import org.belkodevelop.journal.service.JournalService;
import org.belkodevelop.report.domen.PatientReport;
import org.belkodevelop.report.pdf.AbstractITextPdfView;


import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;

import java.net.URL;
import java.util.*;
import java.util.List;

public class PDFPatientReportBuilder extends AbstractITextPdfView {

    private final Log log = LogFactory.getLog(getClass());



    @Override
    protected void buildPdfDocument(Map<String, Object> model, Document doc,
                                    PdfWriter writer, HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        BaseFont times = BaseFont.createFont(request.getSession().getServletContext().getRealPath("/WEB-INF/resources/font/arial.ttf"),
                "cp1251", BaseFont.EMBEDDED);

        PatientReport report = (PatientReport) model.get("patientReport");



        Paragraph paragraph = new Paragraph("Ф.И.О.: ", new Font(times));

        paragraph.add(new Chunk(report.getUser().getFullName()));

        paragraph.add(Chunk.NEWLINE);

        paragraph.add(new Chunk("email: ", new Font(times)));
        paragraph.add(new Chunk(report.getUser().getEmail()));


        paragraph.add(new Chunk("         Телефон: ", new Font(times)));
        paragraph.add(new Chunk(report.getUser().getTelephoneNumber()));

        paragraph.add(Chunk.NEWLINE);

        paragraph.add(new Chunk("Рост: ", new Font(times)));
        paragraph.add(new Chunk(report.getUser().getGrowth().toString()));

        paragraph.add(new Chunk("     Вес: ", new Font(times)));
        paragraph.add(new Chunk(report.getUser().getWeight().toString()));

        doc.add(paragraph);


       /* LineChart chart = new LineChart(new Dimension(300, 300));
        chart.setChartTitle(new ChartTitle("xyLineChart"));
        chart.setEncoder(EncoderFactory.getEncoder(EncodingType.TextEncoding));
        Pair<List<Float>, List<Date>> values1 = new Pair<List<Float>, List<Date>>(Arrays.asList(10f, 20f), Arrays.asList(new Date(), new Date()));




        XYLineChartData data = new XYLineChartData.XYLineChartDataBuilder(values1).color(Color.BLACK).build();

        List<XYLineChartData> d = new ArrayList<XYLineChartData>();
        d.add(data);

        chart.addXYLineChartData(d);


        BufferedImage bufferedImage = (BufferedImage) MiscUtils.getBufferedImage(l);
        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        doc.add(image);  */
        Plot plot = Plots.newPlot(Data.newData(0, 66.6, 33.3, 100));
        LineChart chart = GCharts.newLineChart(plot);
        chart.addHorizontalRangeMarker(33.3, 66.6, Color.BEIGE);
        chart.setGrid(33.3, 33.3, 3, 3);
        chart.addXAxisLabels(AxisLabelsFactory.newAxisLabels(Arrays.asList("Peak","Valley","SDFDSFSDF","SDFSDF","SDFSDFSDFSDFSDFDSFDSFSD","SDFSDFDSFSDF","SDFSDFSDFSDF","SDFSDFSDF"),
                Arrays.asList(33.3,66.6,5,6,7,5,6,6)));
        chart.addYAxisLabels(AxisLabelsFactory.newNumericAxisLabels(0,33.3,66.6,100));
        String url = chart.toURLString();


        BufferedImage bufferedImage = ImageIO.read(new URL(url));

        Image image = Image.getInstance(writer, bufferedImage, 1.0f);
        doc.add(image);

    }

}